package day10;

import java.util.Random;

public class HomeWork {

    public static void main(String[] args) {
        //demo1();
        //demo2();
        demo3();
    }
    public static void demo1(){
        Thread t1=new Thread(new Demo1(2,null),"线程1");
        Thread t2=new Thread(new Demo1(3,t1),"线程2");
        Thread t3=new Thread(new Demo1(5,t2),"线程3");
        t1.start();
        t2.start();
        t3.start();
    }
    public static void demo3(){
        Goods g=new Goods("薄荷糖",10,0);
        new Product(g,"美美薄荷糖").start();
        new Consumer(g,"美美").start();
   }
    public static void demo4(){
        int len=3;
        for(int i=0;i<len;i++){

        }
    }
}
class Goods{
    String name;
    int max;//最大数量
    int reduce;//库存

    public Goods(String name, int max, int reduce) {
        this.name = name;
        this.max = max;
        this.reduce = reduce;
    }
}
class Product extends Thread{
    Goods g;
    String name;
    public Product(Goods g,String name) {//多线程可以共享一个g
        super(name);
        this.g = g;
    }
    public void run() {
        try {
            String name=Thread.currentThread().getName();
            while (true){
                synchronized (g){
                    if(g.reduce<g.max){//库存不足 开始生产
                        //一次生产就生产满
                        System.out.println(name+"开始生产商品"+g.name+",库存"+g.reduce+",容量为"+g.max);
                        Thread.sleep(2000);//模拟生产时间
                        g.reduce=g.max;
                        System.out.println("生产成功,目前库存:"+g.reduce+",顾客可购买");
                    }
                    g.notifyAll();
                    g.wait();
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
class Consumer extends Thread{
    Goods g;
    String name;
    public Consumer(Goods g,String name) {//多线程可以共享一个g
        super(name);
        this.g = g;
    }
    public void run() {
        try {
            Random r=new Random();
            String name=Thread.currentThread().getName();
            while (true){
                synchronized (g){
                  int number = r.nextInt(g.max)+1;
                  if(number>g.reduce){//库存不足 唤醒生产
                      System.out.println(name+"想购买"+number+"个"
                              +g.name+",但库存:"+g.reduce);
                      System.out.println("库存不足，唤醒生产");
                      g.notifyAll();
                      g.wait();
                  }
                  Thread.sleep(500);
                  System.out.println(name+"开始购买"+number+"个"+g.name
                            +",剩余库存:"+(g.reduce-=number));
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Demo1 implements Runnable{
    int n;
    Thread t;
    public Demo1(int n,Thread t){
        this.n=n;
        this.t=t;
    }
    public void run() {
        if(t!=null){
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        String name=Thread.currentThread().getName();
        for(int i=1;i<=100;i++){
            if(i%n==0){
                System.out.println(name+"求出的"+n+"的倍数"+i);
            }
        }
    }
}